layui.config({
    base: '/html/myMoudle/' //假设这是你存放拓展模块的根目录
})
layui.define(function(exports){


    layui.use(['admin', 'carousel'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,carousel = layui.carousel
            ,element = layui.element
            ,device = layui.device();

        //轮播切换
        $('.layadmin-carousel').each(function(){
            var othis = $(this);
            carousel.render({
                elem: this
                ,width: '100%'
                ,arrow: 'none'
                ,interval: othis.data('interval')
                ,autoplay: othis.data('autoplay') === true
                ,trigger: (device.ios || device.android) ? 'click' : 'hover'
                ,anim: othis.data('anim')
            });
        });

        element.render('progress');

    });
    //数据概览
    layui.use(['admin', 'carousel', 'echarts'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,carousel = layui.carousel
            ,echarts = layui.echarts;
        var Arr = [];//名称
        var ArrNum = []//个数
        var dataIntArr=[];//保存转换后的整型字符串
        var name=[];//保存转换后的整型字符串
        $(document).ready(function () {
            $.ajax({
                url: '/hospital/inv/zhi',
                dataType: 'json',
                type: 'get',
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.data.length != 0) {
                        var obj = eval(data.data);//转换为JSON对象
                        for (var i = 0; i < obj.length; i++) {

                            Arr.push(obj[i].name);//存入arr
                            ArrNum.push(obj[i].count);//存入arrnum</span>

                        }

                    }

                    dataIntArr=ArrNum.map(function(data){
                        return +data;
                    });
                    for(j = 0,len=Arr.length; j < len; j++) {
                        name[j]=Arr[j];
                    }
                    console.log(name)

                    console.log(dataIntArr)
                    var echnormcol = [],options = [{
                        title : {
                            text: '药品库存量'
                        },
                        tooltip : {
                            trigger: 'axis'
                        },
                        legend: {
                            data:['库存量']
                        },


                        xAxis : [
                            {
                                type : 'category',
                                data : name,
                                axisLabel: {
                                    interval: 0,
                                    formatter:function(value)
                                    {
                                        return value.split("").join("\n");
                                    }
                                }
                            }
                        ],
                        yAxis : [
                            {
                                type : 'value'
                            }
                        ],
                        series : [
                            {
                                name:'库存量',
                                type:'bar',
                                data:dataIntArr,
                                markPoint : {
                                    data : [
                                        {type : 'max', name: '最大值'},
                                        {type : 'min', name: '最小值'}
                                    ]
                                }
                            }
                        ]
                    }]

                        ,elemNormcol = $('#LAY-index-inv').children('div')
                        ,renderNormcol = function(index){
                        echnormcol[index] = echarts.init(elemNormcol[index], layui.echartsTheme);
                        echnormcol[index].setOption(options[index]);
                        window.onresize = echnormcol[index].resize;
                    };

                    console.log(dataIntArr)


                    if(!elemNormcol[0]) return;
                    renderNormcol(0);
                }

            })
        });



        //监听数据概览轮播
        var carouselIndex = 0;
        carousel.on('change(LAY-index-inv)', function(obj){
            renderDataView(carouselIndex = obj.index);
        });

        //监听侧边伸缩
        layui.admin.on('side', function(){
            setTimeout(function(){
                renderDataView(carouselIndex);
            }, 300);
        });

        //监听路由
        layui.admin.on('hash(tab)', function(){
            layui.router().path.join('') || renderDataView(carouselIndex);
        });
    });


    exports('inv', {})
});